package com.todod.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.todod.common.util.BeanUtilEx;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.dto.AuthorityDto;
import com.todod.entity.QueryEntry;
import com.todod.exception.CustomException;
import com.todod.mapper.AuthorityMapper;
import com.todod.model.Authority;
import com.todod.service.IAuthorityService;
import com.todod.service.IRoleAuthorityService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gsh
 * @since 2020-11-16
 */
@Service
@Transactional
public class AuthorityServiceImpl extends ServiceImpl<AuthorityMapper, Authority> implements IAuthorityService {

	@Autowired
	private IRoleAuthorityService iRoleAuthorityService;
	@Autowired
	private AuthorityMapper authorityMapper;

	@Override
	public List<Authority> getList(QueryEntry queryEntry) {
		QueryWrapper<Authority> queryWrapper = new QueryWrapper<>();
		if (null != queryEntry.getMenuId()) {
			queryWrapper.like("sa_menu_id", queryEntry.getMenuId());
		}
		queryWrapper.orderByDesc("sa_create_time");
		List<Authority> list = list(queryWrapper);
		return list;
	}

	@Override
	public void saveData(Long _userId, Authority authority) {
		if (authority.getId() == null) {
			Authority byCode = getByCodeAndMenuId(authority.getCode(), authority.getMenuId());
			if (byCode != null) {
				throw new CustomException("code已存在");
			}
			authority.setId(SnowflakeIdGenerator.getId());
			authority.setCreateTime(LocalDateTime.now());
			authority.setCreateUserId(_userId);

			save(authority);
		} else {
			Authority u = getById(authority.getId());

			try {
				BeanUtilEx.copyProperties(u, authority);
			} catch (Exception e) {
				log.error("error", e);
			}

			u.setModifyUserId(_userId);
			u.setModifyTime(LocalDateTime.now());

			u.setName(authority.getName());

			updateById(u);
		}

	}

	@Override
	public void delete(Long _userId, Long id) {
		removeById(id);

		iRoleAuthorityService.deleteByAuthorityId(id);
	}

	@Override
	public void deleteByMenuId(Long _userId, Long menuId) {
		QueryWrapper<Authority> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sa_menu_id", menuId);
		remove(queryWrapper);
	}

	@Override
	public Authority getByCodeAndMenuId(String code, Long menuId) {
		QueryWrapper<Authority> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sa_code", code);
		queryWrapper.eq("sa_menu_id", menuId);
		Authority authority = getOne(queryWrapper);
		return authority;
	}

	@Override
	public List<AuthorityDto> getAuthorityListByRoleId(Long roleId) {
		AuthorityMapper authorityMapper = this.getBaseMapper();

		List<AuthorityDto> list = authorityMapper.getAuthorityListByRoleId(roleId);
		return list;
	}

	/**
	 * @Title: editData
	 * @Description:编辑权限
	 * @author MengHaoPeng
	 * @date 2019-10-11
	 * 
	 */
	public void editData(Long userId, Authority authority) {
		authority.setModifyTime(LocalDateTime.now());
		authority.setModifyUserId(userId);
		authorityMapper.editData(authority);
	}

}
